﻿Imports DAO
Imports BUS
Imports DTO
Imports DevComponents.DotNetBar

Public Class frmHoaDonBanSach
    Public Shared MaKH As String
    Public Shared MS As String
    Public Shared DonGia As String

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnTimMaKhachHang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimMaKhachHang.Click
        Dim f As New frmDanhSachKhachHang()
        f.ShowDialog()
        HienThiMaKhachHang()
    End Sub

    Public Sub HienThiMaKhachHang()
        txtMaKH.Text = MaKH
    End Sub

    Public Sub HienThiMaSach()
        txtChiTietMaSach.Text = MS
        txtChiTietDonGia.Text = DonGia
    End Sub

    Public Sub HienThiDSHoaDon()
        dgvDanhSachHoaDon.DataSource = HoaDonDAO.SelectallHoaDon()
    End Sub

    Public Sub HienThiCTDSHoaDon()
        dgvDanhSachHoaDonChiTiet.DataSource = HoaDonDAO.SelectallHoaDonChiTiet()
    End Sub


    Private Sub frmHoaDonBanSach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Size = New Size(430, 727)
        txtChiTietDonGia.Text = "0"
        dtNgayLapHoaDon.Value = DateTime.Now

        ColCmbMaKhachHang.DataSource = KhachHangDAO.GetKhachHangAll()
        ColCmbMaKhachHang.ValueMember = "MaKhachHang"
        ColCmbMaKhachHang.DisplayMember = "HoTenKhachHang"
        ColCmbMaSach.DataSource = SachDAO.SelectSachAll()
        ColCmbMaSach.ValueMember = "MaSach"
        ColCmbMaSach.DisplayMember = "TenSach"
        ColCmbDonGia.DataSource = SachDAO.SelectSachAll()
        ColCmbDonGia.ValueMember = "MaSach"
        ColCmbDonGia.DisplayMember = "GiaBan"

        HienThiDSHoaDon()
        HienThiCTDSHoaDon()
    End Sub

    Private Sub btnTimMaSach_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimMaSach.Click
        Dim f As New frmDanhSachSach()
        f.ShowDialog()
        HienThiMaSach()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim dem As Integer = dgvDanhSachHoaDon.Rows.Count
        Dim hd As New HoaDonDTO()
        'Me.Size = New Size(430, 727)
        Try
            hd.MaKhachHang = Integer.Parse(txtMaKH.Text)
        Catch e1 As FormatException
            MessageBoxEx.Show("Chọn khách hàng từ danh sách")
            btnTimMaKhachHang_Click(sender, e)
            Return
        End Try
        hd.NgayLapHoaDon = dtNgayLapHoaDon.Value
        If HoaDonBUS.ThemHoaDon(hd) = False Then
            MessageBoxEx.Show("Thêm thất bại")
            Return
        End If
        HienThiDSHoaDon()
        txtChiTietMaHoaDon.Text = dgvDanhSachHoaDon.Rows(dem).Cells(0).Value.ToString()
        txtChiTietMaKH.Text = dgvDanhSachHoaDon.Rows(dem).Cells(2).Value.ToString()
    End Sub

    Private Sub dgvDanhSachHoaDon_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDanhSachHoaDon.SelectionChanged
        If dgvDanhSachHoaDon.SelectedRows.Count > 0 Then
            txtMaHoaDon.Text = dgvDanhSachHoaDon.SelectedRows(0).Cells(0).Value.ToString()
            txtChiTietMaHoaDon.Text = dgvDanhSachHoaDon.SelectedRows(0).Cells(0).Value.ToString()
            txtChiTietMaKH.Text = dgvDanhSachHoaDon.SelectedRows(0).Cells(2).Value.ToString()
            txtMaKH.Text = dgvDanhSachHoaDon.SelectedRows(0).Cells(2).Value.ToString()
        End If
    End Sub

    Private Sub dgvDanhSachHoaDonChiTiet_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvDanhSachHoaDonChiTiet.SelectionChanged
        If dgvDanhSachHoaDonChiTiet.SelectedRows.Count > 0 Then
            txtChiTietMaHoaDon.Text = dgvDanhSachHoaDonChiTiet.SelectedRows(0).Cells(0).Value.ToString()
            txtChiTietMaSach.Text = dgvDanhSachHoaDonChiTiet.SelectedRows(0).Cells(1).Value.ToString()
            txtSoLuong.Text = dgvDanhSachHoaDonChiTiet.SelectedRows(0).Cells(2).Value.ToString()
        End If
    End Sub

    Private Sub btnBoSung_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBoSung.Click
        Dim dt As DataTable = ThamSoDAO.GetThamSoAll()
        Dim nomax As Integer = Integer.Parse(dt.Rows(0).ItemArray(3).ToString())
        Dim LuongTonSauKhiBan As Integer = Integer.Parse(dt.Rows(0).ItemArray(4).ToString())
        Dim dt2 As DataTable = ThamSoDAO.SlectTienNoKH(Integer.Parse(txtChiTietMaKH.Text))
        Dim TienNo As Integer = Integer.Parse(dt2.Rows(0).ItemArray(5).ToString())

        Dim hd As New HoaDonDTO()
        Try
            hd.MaHoaDon = Integer.Parse(txtChiTietMaHoaDon.Text)
        Catch e1 As FormatException
            MessageBoxEx.Show("Không được để trống")
            Return
        End Try
        Try
            hd.MaSach = Integer.Parse(txtChiTietMaSach.Text)
        Catch e2 As FormatException
            MessageBoxEx.Show("Chọn mã sách danh sách")
            btnTimMaSach_Click(sender, e)
            Return
        End Try
        Try
            hd.SoLuongMua = UInteger.Parse(txtSoLuong.Text)
        Catch e3 As FormatException
            MessageBoxEx.Show("Nhập vào số lương mua giá trị là số")
        Catch e4 As OverflowException
            MessageBoxEx.Show("Số lượng nhập vào không được âm")
        End Try
        If tienno <= nomax Then
            Dim dt3 As DataTable = ThamSoDAO.SlectSoLuongTon(hd.MaSach)
            Dim luongton As Integer = Integer.Parse(dt3.Rows(0).ItemArray(3).ToString())
            Dim luongmua As Integer = Integer.Parse(txtSoLuong.Text)
            Dim tongtien As Integer = TienNo + (luongmua * Integer.Parse(txtChiTietDonGia.Text))
            If (luongton - luongmua) < LuongTonSauKhiBan Then
                MessageBoxEx.Show("Số lượng tồn của sách này sau khi bán dã nhỏ hơn quy định")
                Return
            End If
            If HoaDonBUS.ThemHoaDonChiTiet(hd) = False Then
                MessageBoxEx.Show("Sách này dã tồn tại")
            Else
                Dim soluongtonmoi As Integer = luongton - luongmua
                Dim s As New SachDTO()
                s.MaSach = Integer.Parse(txtChiTietMaSach.Text)
                s.SoLuongTon = soluongtonmoi
                SachDAO.UpdateSoLuongTon(s)
                Dim kh As New KhachHangDTO()
                kh.MaKhachHang = Integer.Parse(txtChiTietMaKH.Text)
                kh.TienNo = tongtien
                KhachHangDAO.UpdateTienNo(kh)
                Dim bc As New BaoCaoTonDTO()
                bc.MaSach = s.MaSach
                bc.NgayPhatSinh = dtNgayLapHoaDon.Value
                bc.PhatSinh = "Bán sách"
                bc.TonDau = luongton
                bc.TonCuoi = soluongtonmoi
                BaoCaoTonDAO.Insert(bc)
                Dim cn As New BaoCaoCongNoDTO()
                cn.MaKhachHang = kh.MaKhachHang
                cn.NgayPhatSinh = bc.NgayPhatSinh
                cn.NoDau = tienno
                cn.NoCuoi = tongtien
                cn.PhatSinh = "Hóa đơn bán sách"
                BaoCaoCongNoDAO.Insert(cn)
                MessageBoxEx.Show("Thành công", "Kết quả", MessageBoxButtons.OK)
                HienThiCTDSHoaDon()
            End If
        Else
            MessageBoxEx.Show("Tiền nợ của khách đã quá qui đinh để mua sách")
        End If
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Size = New Size(430, 727)
    End Sub

    Private Sub btnThemChiTiet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemChiTiet.Click
        Me.Size = New Size(877, 727)
    End Sub

    Public Sub thanhtien()

        If Integer.Parse(txtChiTietDonGia.Text) > 0 Then
            Dim thanhtien As UInt64 = UInt64.Parse(txtSoLuong.Text) * UInt64.Parse(txtChiTietDonGia.Text)
            txtThanhTien.Text = thanhtien.ToString()
        Else
            MessageBoxEx.Show("Bạn phải chọn sách để lấy đơn giá")
            Return
        End If
    End Sub

    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        txtSoLuong.Clear()
    End Sub

    Private Sub txtSoLuong_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSoLuong.Leave
        thanhtien()
    End Sub
End Class